package com.shenrongrong.service.impl;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shenrongrong.mapper.UserMapper;
import com.shenrongrong.model.User;
import com.shenrongrong.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public List<User> selectUser(User user) {
        return userMapper.selectUser(user);
    }
    @Override
    @Transactional
    public PageInfo<User> selectUser(Integer pageNum, Integer pageSize, User user) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList = userMapper.selectUser(user);
        return new PageInfo<User>(userList);
    }

    @Override
    @Transactional
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    @Transactional
    public int deleteUser(Integer id) {
        return userMapper.deleteUser(id);
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public boolean isPhoneUnique(String phone) {
        User queryUser = new User();
        queryUser.setPhone(phone);
        List<User> userList = userMapper.selectUser(queryUser);
        // 返回 true 表示手机号已存在，false 表示不存在（可注册）
        return userList != null && !userList.isEmpty();
    }
    @Override
    public boolean isUsernameUnique(String username) {
        User queryUser = new User();
        queryUser.setUsername(username);
        List<User> userList = userMapper.selectUser(queryUser);
        return userList != null && !userList.isEmpty();
    }

    @Override
    public boolean isUserUnique(String username, String password, Integer role) {
        User queryUser = new User();
        queryUser.setUsername(username);
        queryUser.setPassword(password);
        queryUser.setRole(role);
        List<User> userList = userMapper.selectUser(queryUser);
        if(userList.size() == 1){
            System.out.println("ture");
            return true;
        }
        System.out.println("false");
        return false;
    }


}
